<template>
	<om-addpage style="padding-top: 35px;" :busCode="busCode" :menuName="menuName" @formSubmit="formSubmit">
		<om-autoform ref="autoform" :initConfigData="mainConfigData" :initValue="initValue"></om-autoform>
	</om-addpage>
</template>

<script>
	import {
		errorModal,
		ignoreHtml,
		getUrl,
		confirmModal,
		ptFloat,
		minusF
	} from '@/static/js/common.js';
	import {
		changeConfigAttr,
		buildFormData,
		formValidation,
		tranferToEditors,
		changeEditor,
		setSubmitSubValue,
		configSelectData,
		hasValue,
		formShowLoading,
		getFormFields,
		hasEditor,
		getEditorTitle,
		formModelLoad,
		getWorkCacheKey
	} from '@/static/js/formUtil.js';
	import urlUtil from '@/static/js/urlUtil.js';
	import { mapState } from 'pinia';
	import {useLoginUserStore} from '@/stores/login-user.js';
	export default {
		provide() {
			return {
				formPage: this
			};
		},
		data() {
			return buildFormData({
				infoCacheKey:"hr-kq-goOutWork-info-cache",
				sameOutUserCache:'hr-kq-goOutWork-sameOutUser-cache',
				userId1:'',
				editId:"",
				fileKey:'40',
				defaultDataCache:'hr-kq-goOutWork-defaultData-cache',
				defaultDataCache1:'hr-kq-goOutWork-defaultData1-cache'
			})
		},
		computed: {
			...mapState(useLoginUserStore, ['userName', 'userId'])
		},
		methods:{
			getWorkParam(){
				let _that = this
				return {
					userId:_that.$refs.autoform.formdata.infoId,
					editId:_that.editId,
					dataStr:JSON.stringify(_that.subEditorObj['title1'].workList)
				}
			},
			formSubmit(){
				let _that = this
				delete _that.formdata.bottomFileUpload
				delete _that.formdata.bottomFileList
				_that.formdata.updateBottomFileFlag= true
				if(Object.prototype.toString.call(_that.$refs.autoform.formdata.bottomFileList) == "[object String]"){
					_that.formdata.bottomFileListStr = _that.$refs.autoform.formdata.bottomFileList|| ''
				}else{
					_that.formdata.bottomFileListStr = JSON.stringify(_that.$refs.autoform.formdata.bottomFileList || []) || ''
					
				}
				delete _that.formdata.bottomFileUpload
				delete _that.formdata.bottomFileList
				if(!_that.edit){
					_that.om.formSave("goOutWork/doAdd",_that.formdata,_that)
				}else{
					_that.om.formSave("goOutWork/doUpdate",_that.formdata,_that)
				}
			},
			verifyWorkOffice(){
				let _that = this
				if(!_that.$refs.autoform.formdata.infoId){
					let title = _that.$refs.autoform.getEditorTitle("title0","infoId")
					_that.om.toast("请先选择"+title)
					return false
				}else{
					return true
				}
			},
			verifySameOutUser(){
				let _that = this
				if(!_that.$refs.autoform.formdata.infoId){
					let title = _that.$refs.autoform.getEditorTitle("title0",'infoId')
					_that.om.toast('请先选择'+title)
					return false
				}else{
					return true
				}
			},
			getUserSalaryPosInfo(id){
				let _that = this
				_that.om.request(
					"orgInstPosUserOfficial/getUserSalaryPosInfo",
					{
						userId:id
					}
				).then(res=>{
					_that.$refs.autoform.formdata.salaryPosId = res.value.posId
					_that.$refs.autoform.formdata.salaryPosName = res.value.posName
					_that.$refs.autoform.formdata.rinstId = res.value.instId
					_that.$refs.autoform.formdata.rinstName = res.value.instName
					_that.$refs.autoform.formdata.parentUserId = res.value.parentUserId
					_that.$refs.autoform.formdata.parentUserName = res.value.parentUserName
					
					_that.om.request(
						"tinstitution/getInstInfo",
						{
							instId:res.value.instId
						}
					).then(res=>{
						_that.$refs.autoform.formdata.instLevelId =  res.value.instcatId
						_that.$refs.autoform.formdata.instLevelName =  res.value.instcatName
					})
				})
			},
			initLoad(options){
				let _that = this
				_that.simpleClazzName = options.simpleClazzName
				let ajaxArr = []
				let fileId = options.fileId
				if(fileId){
					_that.editId = fileId
					ajaxArr.push({
						url:"goOutWork/getValue",
						param:{
							fileId:fileId
						}
					})
				}
				formModelLoad(ajaxArr,function(res,mainData){
					changeEditor("topinfo","zuserId",{
						defaultName: _that.userName,
						defaultValue: _that.userId
					},null,mainData)
					changeEditor("title0","infoId",'isSearchUser',true,mainData)
					changeEditor("title0","infoId",{
						link:"../../public/searchUser/searchUser",
						"linkParam":{
							namefield: urlUtil.userPageUrl().namefield,
							searchfield: "q",
							cachename: _that.infoCacheKey,
							defaultDataCache:_that.defaultDataCache,
							isNew:true,
							isSingle:true,
							defaultData:'searchUserDefaultData',
							placeholder: urlUtil.userPageUrl().prompt,
							pagination: true
						}
					},null,mainData)
					changeEditor('title0', 'sameOutUserId','isSearchUser', true, mainData);
					changeEditor('title0', 'sameOutUserId', {
						'link':'../../public/searchUser/searchUser',
						linkParam: {
							namefield: urlUtil.userPageUrl().namefield,
							searchfield: "q",
							cachename: _that.sameOutUserCache,
							defaultDataCache:_that.defaultDataCache1,
							isNew:true,
							defaultData:'searchUserDefaultData',
							placeholder: urlUtil.userPageUrl().prompt,
							pagination: true
						}
					}, null, mainData);
					if(_that.subEditorObj["title1"]){
						let att = {
							getWorkParam:"getWorkParam"
						}
						let configData = _that.subConfigData["title1"]
						if(hasEditor("title1","startTime",configData)){
							att.titleTemplate = "<%= startTime || '未填写'%>"
						}
						if(hasEditor("title1","endTime",configData)){
							att.titleTemplate += "~<%= endTime || '未填写'%>"
						}
						if(hasEditor("title1","street",configData)){
							let title = getEditorTitle("title1","street",configData)
							att.subtitle1Template = title + ":<%= street || '未填写'%>"
						}
						if(hasEditor("title1","reason",configData)){
							let title = getEditorTitle("title1","reason",configData)
							att.subtitle2Template = title + ":<%= reason || '未填写'%>"
						}
						changeEditor("title1","dataStr",att,mainData)
					}
					let tempFileList = []
					let fileUrlList = []
					changeEditor('bottominfo','bottomFileUpload','fileUploadKey',"40",mainData)
					changeEditor("bottominfo","bottomFileUpload","tempFileList", tempFileList,mainData)
					changeEditor("bottominfo","bottomFileUpload","fileUrlList", fileUrlList,mainData)
					
					if(fileId){
						let fieldArr = getFormFields(mainData)
						fieldArr.push("id")
						for(let index in res[0].value){
							if(fieldArr.includes(index)){
								_that.$refs.autoform.$set(_that.$refs.autoform.formdata,index,res[0].value[index])
								_that.$set(_that.formdata,index,res[0].value[index])
							}
						}
						if(_that.formdata.bottomFileList){
							let tempFileList = []
							let fileUrlList = _that.formdata.bottomFileList.map(t=>{
								tempFileList.push(t.fileSaveName)
								return t.fileSaveName
							})
							changeEditor("bottominfo","bottomFileUpload","tempFileList", tempFileList,mainData)
							changeEditor("bottominfo","bottomFileUpload","fileUrlList", fileUrlList,mainData)
						}
						let arr = []
						if(res[0].value.sameOutUserId && res[0].value.sameOutUserJoinName && res[0].value.sameOutUserName){
							let userIdList = res[0].value.sameOutUserId.split(",")
							let strArr = res[0].value.sameOutUserJoinName.split(",")
							let userNameList = res[0].value.sameOutUserName.split(",")
							for(let index in strArr){
								let sArr = strArr[index].split("/")
								let userPosName = (sArr[0]+'/'+sArr[1])
								let obj = {
									userPosName:userPosName.substring(0,userPosName.length-1),
									username:userNameList[index],
									id:userIdList[index]
								}
								arr.push(obj)
							}
							changeEditor('title0', 'sameOutUserId', {
								initSelect: arr
							},null,mainData)
						}
						_that.getUserSalaryPosInfo(_that.$refs.autoform.formdata.infoId)
						let dataList = _that._.cloneDeep(res[0].value.dataList)
						_that.subEditorObj["title1"].workList = dataList
						_that.$refs.autoform.$set(_that.$refs.autoform.formdata,"dataStr",JSON.stringify(dataList))
						_that.$set(_that.formdata,"dataStr",JSON.stringify(dataList))
					}
					_that.$nextTick(function(){
						if(!_that.$refs.autoform.formdata.infoId){
							_that.$refs.autoform.formdata.infoId = _that.userId
							_that.$refs.autoform.formdata.infoName = _that.userName
							_that.getUserSalaryPosInfo(_that.userId)
						}
						_that.verifySameOutUser()
					})
				},options)
			}
		},
		onLoad(options) {
			let _that = this
			_that.initLoad(options)
		},
		onUnload(){
			let _that = this;
			uni.removeStorage({
				key:_that.defaultDataCache
			})
			uni.removeStorage({
				key:_that.defaultDataCache1
			})
			//#ifdef APP-PLUS
			if (_that.$refs.autoform) {
				for (var fieldId in _that.subEditorObj) {
					uni.removeStorage({
						key: getWorkCacheKey(fieldId)
					});
				}
			}
			//#endif
		},
		onShow() {
			let _that = this
			if(_that.$refs.autoform){
				for(let fileId in _that.subEditorObj){
					let editor = _that.subEditorObj[fileId]
					uni.getStorage({
						key:getWorkCacheKey(fileId),
						success:(res)=>{
							editor.workList = res.data
							uni.removeStorage({
								key:getWorkCacheKey(fileId)
							})
						}
					})
				}
				uni.getStorage({
					key:_that.infoCacheKey,
					success:(res)=>{
						if(res.data){
							let d = res.data[0]
							let formdata = _that.$refs.autoform.formdata
							formdata.infoId = d.id
							formdata.infoName = d.username
							_that.userId1 = d.id
							_that.getUserSalaryPosInfo(d.id)
							uni.removeStorage({
								key:_that.infoCacheKey
							})
						}
					}
				})
				uni.getStorage({
					key:_that.sameOutUserCache,
					success:(res)=>{
						if(res.data){
							let d = res.data
							uni.setStorage({
								key:_that.defaultDataCache1,
								data:d
							})
							let formdata = _that.$refs.autoform.formdata;
							if(d.length){
								let idList = []
								let nameList = []
								let sameOutUserIds = ''
								for(let index in d){
									if(d[index].id == formdata.infoId){
										errorModal(_that,'提示系信息','请勿选择当前出差人作为同行人',()=>{
											uni.removeStorage({
												key:_that.sameOutUserCache
											})	
											uni.removeStorage({
												key:_that.defaultDataCache
											})	
										}) 
										return
									}
									idList.push(d[index].id)
									nameList.push(d[index].username)
								}
								
								let idStr = idList.map(e =>e).join(",")
								let nameStr = nameList.map(e =>e).join(",");
								
								formdata.sameOutUserId = idStr
								formdata.sameOutUserName = nameStr
								
							}
							uni.removeStorage({
								key:_that.sameOutUserCache
							})
						}
					}
				})
			}
		}
	}
</script>
<style></style>                                                                                                                                                                                                                                                                                                                  
